/*
 ============================================================================
 Name        :	w.h
 Author      :	NAYED (nizar.ayed@fullcommedia.com)
 Version     :	0.1
 Copyright   :	Code delivered as is with no liability. No commercial use
				is allowed without written authorization from the Author.
 Description :
	 To understand the following code and structure:
	 - An "object" is a "clone" of one "prototype" and has one "owner"
	 - "Nil" is nothing and of course, has no prototype
	 - "Root" is the only clone of Nil
	 - "Root" is the generic prototype of everything
	 - By default, a prototype is the owner of his clones
	 
	 As a results, we have:
	 - An object is a "property" of his owner 
	 - An object is described by:
	 + his "owned" properties (the objects he owns)
	 + his "borrowed" properties from his prototype (attn. recursive notion)
	 - An object owned by "Nil" is an "orphan":
	 + No inheritance of orphans
	 + Orphans can be killed
	 - An object owned by "Root" is a "principle"
	 
	 Object operations:
	 - "clone" : create an object from a prototype
	 - "kill" : destory an object (an orphan)
	 - "own" : assign an object as new property to another object
	 - "borrow" : (let's keep this for the future - here we will deal with contracts)
 ============================================================================
 */

#include <stdlib.h>
#include <stdio.h>

#ifndef W_H_
#define W_H_

/* Max Number of objects in the memory */
#define MAX_MEM 32000

/* FLAGS (32 bits) */
#define STANDARD_OBJECT 0x0000	/* By default, we don't check standard objects */
#define NIL_OBJECT		0x0001	
#define ROOT_OBJECT		0x0002

/* The object structure */
typedef struct {
	void *proto_id;	/* Origin of inherited properties */
	void *owner_id;	/* Owner of the object (or clone) */
	int flags;		/* Activation flags of the object - can help for garbage collector  */
	int size;		/* Size of the payload - This is int: 32 bits it's more than enough */
	char *name;		/* Human friendly reference to the object */
	char *payload;	/* The "content" of the object - it's "soul" that should be compiled :-) */
	/* The object id is the pointer to the object itself. */
} object;

/* Globals - always available */
object *universe;	/* Where everything resides */
object *nil;		/* This is Nothing and ... Everything! NL is his own clone*/
object *root;		/* The start to everything. It is a clone of NIL */
/* Context stack. Each object is a context. Used as a compile scope */
object *context[MAX_MEM]; /* At start, NIL is the context for everything */

/* Counters */
int object_number = 0;
int context_number = 0;

/* Initialization of the wVM */
void wVM_initialize();
void wVM_init_context();

/* Base */
void _nil();
void _root();
object* _clone(object *, char *);
object* clone_by_name(char *, char *);

/* Object manipulation */
object* _get_property(char *);
void _own_property(object*, object*);
void own_property_by_name(object*, char *property_name);
void _set_payload(object *, char *);
void set_payload_by_name(char *, char  *);
void _dump(object *);
void dump_by_name(char *);
void dump_all();

#endif /* W_H_ */
